<?php include '../template/template_main.tpl'; 
require("../connect/connect.php");
require ("../admin/insertProb.php");

if(isset($_SESSION["profile"])){
	switch($_SESSION["profile"]){
		case 2: break;
		default: header("Location: index.php");
	}
}
else
header("Location: comoFazerLogin.php");

if(isset($_GET["id"]))
	$id = $_GET["id"];

if(isset($_POST["submit"])){
	$error = array();
	
	if($_POST["probTitulo"] == "")
		$error[] = "Deve preencher o titulo.";
	if($_POST["prob_text"] == "")
		$error[] = "Deve preencher o problema.";
	if($_POST["resposta"] == "")
		$error[] = "Deve preencher a resposta.";
	if($_POST["sugestoes"] == "")
		$error[] = "Deve preencher as sugestões.";
	if (count($error) == 0){
		updateProb($id, $conn);	
	}
}

$prob = getProb($id, $conn);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../css/template.css" />
<link rel="stylesheet" type="text/css" href="../css/corpo_main.css" />
<script type="text/javascript" src="../js/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="../js/main.js" ></script>

<title>.:MateMatrix:.</title>

<script>

function editEnable(){
    $("#probTitulo").attr("disabled", false);
    $("#prob_text").attr("disabled", false);
    $("#resposta").attr("disabled", false);
    $("#sugestoes").attr("disabled", false);
    $("#submit").attr("disabled", false);
    $("#edit").attr("disabled", "disabled");
}

$(document).ready(function() {
    $("#probTitulo").attr("disabled", "disabled");
    $("#prob_text").attr("disabled", "disabled");
    $("#resposta").attr("disabled", "disabled");
    $("#sugestoes").attr("disabled", "disabled");
    $("#submit").attr("disabled", "disabled");
});
</script>

</head>
<body>
	<?php makeHeader(); ?>
	<?php echo $estruturaInicio;?>
	<div id="title">
		<h1>- Criar Problema -</h1>
	</div>
	<div id="problemaDiv">
			<?php
			if (isset($error) && (count($error) > 0))
			{
				echo "<div class=\"erros\"><ul>";
				foreach ($error as $value) {
					printf("<li>%s</li>", $value);
				}
				echo "</ul></div>";
			}
			if (isset($mensagem))
			{
				echo "<div class=\"info\"><ul><li>$mensagem</li></ul></div>";
			}
		?>
		<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?id=<?php echo $id;?>" method="post">
			<div>
				Título:<br />
			</div>
			<div>
				<input type="text" id="probTitulo" name="probTitulo" value="<?php if (isset($_POST["probTitulo"])) echo htmlspecialchars($_POST["probTitulo"]); else echo htmlspecialchars($prob["titulo"]); ?>"/>
			</div>
			<div>
				Problema:<br />
				<textarea id="prob_text" name="prob_text" rows="10" cols="100"><?php if (isset($_POST["prob_text"])) echo htmlspecialchars($_POST["prob_text"]); else echo htmlspecialchars($prob["problema"]); ?></textarea>
			</div>
			<div>
				Resposta:<br />
				<textarea id="resposta" name="resposta" rows="10" cols="100"><?php if (isset($_POST["resposta"])) echo htmlspecialchars($_POST["resposta"]); else echo htmlspecialchars($prob["resposta"]);?></textarea>
			</div>
				<div>
				Sugestões:<br />
				<textarea id="sugestoes" name="sugestoes" rows="10" cols="100"><?php if (isset($_POST["sugestoes"])) echo htmlspecialchars($_POST["sugestoes"]); else echo htmlspecialchars($prob["sugestoes"]); ?></textarea>
			</div>
			<div>
				<input type="submit" value="Guardar" name="submit" id="submit" />
				<input type="button" value="Editar" onclick="editEnable()" id="edit" />
			</div>
		</form>
	</div>
	<?php echo $estruturaFim; ?>
	<?php echo $footer;?>
</body>
</html>